#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Models/Shortrate/Onefactormodels/Vasicek.h>
#include <gen/QL/Models/TermStructureConsistentModel.h>
#pragma unmanaged 
#include <ql\models\shortrate\onefactormodels\hullwhite.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Models { namespace Shortrate { namespace Onefactormodels {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IHullWhite
	public ref class CHullWhite  : 
            public CVasicek,
            public Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite
	{
	protected: 
		boost::shared_ptr<QuantLib::HullWhite>* _ppHullWhite;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::HullWhite>* _phHullWhite;
#endif
		Object^ _HullWhiteOwner;     // reference to object that manages the storage for this object
	internal:
		CHullWhite (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma);
        CHullWhite (boost::shared_ptr<QuantLib::HullWhite>& childNative, Object^ owner);
        CHullWhite (QuantLib::HullWhite& childNative, Object^ owner);
        CHullWhite (CHullWhite^ copy);
        CHullWhite (PLATFORM::Type^ t);
#ifdef STRUCT
        CHullWhite (QuantLib::HullWhite childNative);
#endif       
#ifdef HANDLE
		CHullWhite (QuantLib::Handle<QuantLib::HullWhite>& childNative, Object^ owner);
		CHullWhite (QuantLib::Handle<QuantLib::HullWhite> childNative);
#endif
		virtual ~CHullWhite ();
		!CHullWhite ();

	internal:
		QuantLib::HullWhite& GetReference ();
		boost::shared_ptr<QuantLib::HullWhite>& GetShared ();
		QuantLib::HullWhite* GetPointer ();
        void SetHullWhite (boost::shared_ptr<QuantLib::HullWhite> native)
        {
            if (_ppHullWhite != NULL)
                delete _ppHullWhite;
            _ppHullWhite = new boost::shared_ptr<QuantLib::HullWhite> (native);
            SetVasicek (boost::dynamic_pointer_cast<QuantLib::Vasicek> (*_ppHullWhite));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::HullWhite>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Double ConvexityBias (Double futurePrice, Double t, Double T2, Double sigma, Double a) ;
		virtual Double DiscountBondOption (QL::Option::TypeEnum type, Double strike, Double maturity, Double bondMaturity) ;
		virtual Cephei::QL::ILattice^ Tree (Cephei::QL::ITimeGrid^ grid) ;
    private:
        Cephei::QL::Models::CTermStructureConsistentModel^ _TermStructureConsistentModel;
    private:
        property CTermStructureConsistentModel^ TermStructureConsistentModel
        {
            CTermStructureConsistentModel^ get ()
            {
                if (_TermStructureConsistentModel == nullptr)
                {
                    _TermStructureConsistentModel = REFNEW CTermStructureConsistentModel (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppHullWhite), this);
                }
                return _TermStructureConsistentModel;
            }
        }
    public:
        operator Cephei::QL::Models::ITermStructureConsistentModel^ ()
        {
            return static_cast<Cephei::QL::Models::ITermStructureConsistentModel^>(TermStructureConsistentModel);
        }
        operator Cephei::QL::Models::CTermStructureConsistentModel^ ()
        {
            return TermStructureConsistentModel;
        }
        
    public:
        property Cephei::QL::Termstructures::IYieldTermStructure^ M1560_4591 
        {
            virtual Cephei::QL::Termstructures::IYieldTermStructure^ get () = Cephei::QL::Models::ITermStructureConsistentModel::TermStructure::get
            {
                return static_cast<Cephei::QL::Models::ITermStructureConsistentModel^>(TermStructureConsistentModel)->TermStructure;
            }
        }
        virtual Cephei::QL::ILattice^ M1560_Tree (Cephei::QL::ITimeGrid^ prm1)
        {
            return ((Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^)this)->Tree ( prm1);
        }
        virtual Double M1560_DiscountBondOption (QL::Option::TypeEnum type, Double strike, Double maturity, Double bondMaturity)
        {
            return ((Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^)this)->DiscountBondOption ( type,  strike,  maturity,  bondMaturity);
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^>::typeid)]
	[FactoryFor(Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite::typeid)]
	[FactoryFor(Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite_Factory::typeid)]
	public ref class CHullWhite_Factory sealed : public IHullWhite_Factory
	{
	public:
        virtual IHullWhite^ Create (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma);
    };
   
/*Cephei*/ } /*QL*/ } /*Models*/ } /*Shortrate*/ } /*Onefactormodels */}
